package com.sc.nft.admin.controller;

import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.vo.PushManageTagVo;
import com.sc.nft.entity.vo.PushManageVo;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.PushService;
import com.sc.nft.service.PushTagService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @author 刘国航
 * @since 2024/6/4 9:29
 */
@RestController
@Api(tags = "推送管理")
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/pushManage")
public class PushManageController {

    private final PushService pushService;
    private final PushTagService pushTagService;

    @PostMapping(value = "/addTagByCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据藏品新增标签", notes = "根据藏品新增标签", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addTagByCollection(@Valid @RequestBody PushManageAddTagByCollectionRequest request) {
        pushTagService.addTagByCollection(request.getName(), request.getCollectionId());
        return Result.success();
    }

    @PostMapping(value = "/uploadTagUserExcel", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据EXCEL上传文件标签用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "uploadFile", value = "文件流对象", required = true,dataType = "__File")})
    @ResponseBody
    @SysMemberLogin
    public Result<List<String>> uploadTagUserExcel(MultipartFile uploadFile) throws IOException {
        return Result.success(pushTagService.uploadTagUserExcel(uploadFile));
    }

    @PostMapping(value = "/addTagByExcel", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据EXCEL上传文件新增标签", notes = "根据EXCEL上传文件新增标签", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addTagByExcel(@Valid @RequestBody PushManageAddTagByExcelRequest request) throws IOException {
        pushTagService.addTagByExcel(request.getName(), request.getUserTelList());
        return Result.success();
    }

    @PostMapping(value = "/tag/listPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询标签列表", notes = "查询标签列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<PushManageTagVo> tagListPage(@RequestBody PushManageTagListPageRequest request) {
        return pushTagService.tagListPage(request.getPageNo(), request.getPageSize(), request.getName());
    }

    @PostMapping(value = "/tag/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询标签详情", notes = "查询标签详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<PushManageTagVo> tagDetail(@Valid @RequestBody PushManageTagDetailRequest request) {
        return Result.success(pushTagService.tagDetail(request.getPageNo(), request.getPageSize(), request.getId(), request.getUserTel()));
    }

    @PostMapping(value = "/allTag", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "全部标签", notes = "推送详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<PushManageTagVo>> allTag() {
        return Result.success(pushTagService.allTag());
    }

    @PostMapping(value = "/tag/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除标签", notes = "查询标签详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result tagDelete(@Valid @RequestBody IdRequest request) {
        pushTagService.tagDelete(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/tag/addUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增标签下用户", notes = "新增标签下用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result tagAddUser(@Valid @RequestBody PushManageTagAddUserRequest request) {
        pushTagService.tagAddUser(request.getId(), request.getUserTelList());
        return Result.success();
    }

    @PostMapping(value = "/tag/deleteUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除标签下用户", notes = "删除标签下用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result tagDeleteUser(@Valid @RequestBody PushManageTagDeleteUserRequest request) {
        pushTagService.tagDeleteUser(request.getId(), request.getUserId());
        return Result.success();
    }

    @PostMapping(value = "/addPush", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增推送", notes = "新增推送", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addPush(@Valid @RequestBody PushManageAddEditPushRequest request) {
        pushService.addPush(request.getPushTitle(), request.getPushContent(), request.getPushIcon(), request.getPushType(), request.getEnableSchedulePush(), request.getSchedulePushTime(), request.getPushTagIdList());
        return Result.success();
    }

    @PostMapping(value = "/editPush", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑推送", notes = "编辑推送", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result editPush(@Valid @RequestBody PushManageAddEditPushRequest request) {
        pushService.editPush(request.getId(), request.getPushTitle(), request.getPushContent(), request.getPushIcon(), request.getPushType(), request.getEnableSchedulePush(), request.getSchedulePushTime(), request.getPushTagIdList());
        return Result.success();
    }

    @PostMapping(value = "/deletePush", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除推送", notes = "删除推送", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deletePush(@Valid @RequestBody IdRequest request) {
        pushService.deletePush(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/listPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "推送列表", notes = "推送列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<PushManageVo> listPage(@RequestBody PushManageListPageRequest request) {
        return pushService.listPage(request.getPageNo(), request.getPageSize(), request.getPushTitle());
    }

    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "推送详情", notes = "推送详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<PushManageVo> detail(@RequestBody IdRequest request) {
        return Result.success(pushService.detail(request.getId()));
    }

    @PostMapping(value = "/updateStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "推送上下架", notes = "推送详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<PushManageVo> updateStatus(@Valid @RequestBody PushManageUpdateStatusRequest request) {
        pushService.updateStatus(request.getId(), request.getStatus(), SysMemberPool.getMember());
        return Result.success();
    }

}
